﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Offices.Services;
using Autorent.Desktop.Modules.Offices.Views;

namespace Autorent.Desktop.Modules.Offices.ViewModels
{
    public class OfficesListViewModel : ViewModelBase
    {
        #region Public Properties

        public OfficesListView View { get; set; }

        public ObservableCollection<Office> Offices
        {
            get
            {
                return _repository.GetOffices();
            }
        }

        public Office SelectedOffice { get; set; }

        #endregion

        #region Constructors

        public OfficesListViewModel(OfficesListView view, IOfficesRepository repository, IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            View = view;
            view.Model = this;

            _repository = repository;
            _regionManager = regionManager;
            _container = container;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<CollectionChangedEvent>().Subscribe(new Action<string>(OnPropertyChanged));
        }

        #endregion

        #region Commands

        public ICommand CreateOffice
        {
            get
            {
                return new RelayCommand(param => createOffice());
            }
        }

        public ICommand UpdateOffice
        {
            get
            {
                return new RelayCommand(param => updateOffice());
            }
        }

        public ICommand DeleteOffice
        {
            get
            {
                return new RelayCommand(param => deleteOffice());
            }
        }

        public ICommand SearchOffice
        {
            get
            {
                return new RelayCommand(param => searchOffice());
            }
        }

        #endregion

        #region Private Variables

        private IOfficesRepository _repository;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void createOffice()
        {
            if (_regionManager.Regions["DetailsRegion"].GetView("CreateOfficeView") == null) 
            {
                foreach (var view in _regionManager.Regions["DetailsRegion"].Views)
                {
                    _regionManager.Regions["DetailsRegion"].Remove(view);
                }

                _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<CreateOfficeViewModel>().View, "CreateOfficeView"); 
            }
        }

        private void updateOffice()
        {
            if (SelectedOffice != null)
            {
                if (_regionManager.Regions["DetailsRegion"].GetView("UpdateOfficeView") == null) 
                {
                    foreach (var view in _regionManager.Regions["DetailsRegion"].Views)
                    {
                        _regionManager.Regions["DetailsRegion"].Remove(view);
                    }

                    _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<UpdateOfficeViewModel>(new ParameterOverrides() { { "office", SelectedOffice }}).View, "UpdateOfficeView"); 
                }
            }
            else
            {
                MessageBox.Show("Select office!", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void deleteOffice()
        {
            _repository.DeleteOffice(SelectedOffice);
            OnPropertyChanged("Offices");
        }

        private void searchOffice()
        {
            if (_regionManager.Regions["SearchRegion"].GetView("SearchOfficeView") == null)
            {
                _regionManager.Regions["SearchRegion"].Add(_container.Resolve<SearchOfficeViewModel>().View, "SearchOfficeView");
            }
        }

        #endregion
    }
}
